﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Esint.Common.Web
{
    public abstract class JsHelper
    {
        #region 窗口操作类

        /// <summary>
        /// 窗口操作类
        /// </summary>
        public class Window
        {
            protected const string OPEN_WINDOW_SIZE = "'left='+(screen.width/2-{0}/2)+'px,top='+(screen.height/2-{1}/2)+'px,height={1},width={0},toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=no'";
            protected const string OPEN_DIALOG_SIZE = "'dialogLeft='+(screen.width/2-{0}/2)+'px;dialogTop='+(screen.height/2-{1}/2)+'px;dialogHeight={1}px;dialogWidth={0}px;toolbar=no; location=no; status=no; menubar=no; scrollbars=yes; resizable=no'";

            #region 打开窗口

            /// <summary>
            /// 功能描述：弹出新页面，并使页面居中
            /// </summary>
            /// <param name="url">新页面URL</param>
            /// <param name="width">新页面宽度</param>
            /// <param name="higth">新页面高度</param>
            public static void OpenWindow(string url, int width, int height)
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", "window.open('" + url + "',''," + string.Format(OPEN_WINDOW_SIZE, width, height) + ");", true);
            }

            /// <summary>
            /// 设置LinkButton打开窗口代码
            /// </summary>
            /// <param name="lbtn">LinkButton或Button</param>
            /// <param name="url"></param>
            /// <param name="width"></param>
            /// <param name="height"></param>
            public static void OpenWindow(System.Web.UI.WebControls.WebControl lbtn, string url, int width, int height)
            {
                if (lbtn is LinkButton)
                {
                    ((LinkButton)lbtn).OnClientClick = "window.open('" + url + "',''," + string.Format(OPEN_WINDOW_SIZE, width, height) + ");";
                }

                if (lbtn is Button)
                {
                    ((Button)lbtn).OnClientClick = "window.open('" + url + "',''," + string.Format(OPEN_WINDOW_SIZE, width, height) + ");";
                }
            }

            #endregion

            #region 打开模态窗口

            /// <summary>
            /// 功能描述： 打开模式对话框
            /// </summary>
            /// <param name="url"></param>
            /// <param name="width"></param>
            /// <param name="higth"></param>
            public static void OpenModalDialog(string url, int width, int height)
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", "var val = window.showModalDialog('CommonFrame.aspx?url=" + url + "',''," + string.Format(OPEN_DIALOG_SIZE, width.ToString(), height.ToString()) + ");", true);
            }
            /// <summary>
            /// 设置LinkButton打开窗口代码
            /// </summary>
            /// <param name="lbtn"></param>
            /// <param name="url"></param>
            /// <param name="width"></param>
            /// <param name="height"></param>
            public static void OpenModalDialog(System.Web.UI.WebControls.LinkButton lbtn, string url, int width, int height)
            {
                lbtn.OnClientClick = "var val = window.showModalDialog('../CommonFrame.aspx?url=" + url + "',''," + string.Format(OPEN_DIALOG_SIZE, width.ToString(), height.ToString()) + ");";
            }


            /// <summary>
            /// 设置Button打开窗口代码
            /// </summary>
            /// <param name="lbtn"></param>
            /// <param name="url"></param>
            /// <param name="width"></param>
            /// <param name="height"></param>
            public static void OpenModalDialog(System.Web.UI.WebControls.Button btn, string url, int width, int height)
            {
                btn.OnClientClick = "var val = window.showModalDialog('../CommonFrame.aspx?url=" + url + "',''," + string.Format(OPEN_DIALOG_SIZE, width.ToString(), height.ToString()) + ");";
            }
            #endregion

            #region 关闭窗口

            
            public static void CloseSelf()
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", "window.close(); ", true);
            }

            /// <summary>
            /// Closes the parent.
            /// </summary>
            /// <returns></returns>
            public static void CloseParent()
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", "if (window.parent) { window.parent.close(); } ", true);
            }

            /// <summary>
            /// Closes the opener.
            /// </summary>
            /// <returns></returns>
            public static void CloseOpener()
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", "if (window.opener) { window.opener.close(); } ", true);
            }
            #endregion

            #region 刷新窗口
            /// <summary>
            /// Refreshes the self.
            /// </summary>
            /// <returns></returns>
            public static void RefreshSelf()
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", " window.location += ' ';  ", true);
            }

            /// <summary>
            /// Refreshes the opener.
            /// </summary>
            /// <returns></returns>
            public static void RefreshOpener()
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", " if (window.opener) { window.opener.location += ' '; } ", true);
            }

            /// <summary>
            /// Refreshes the parent.
            /// </summary>
            /// <returns></returns>
            public static void RefreshParent()
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", " if (window.parent) { window.parent.location += ' '; } ", true);
            }
            #endregion

            #region 后退
            /// <summary>
            /// Selfs the go back.
            /// </summary>
            /// <returns></returns>
            public static void SelfGoBack()
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", " window.history.back();  ", true);
            }

            /// <summary>
            /// Parents the go back.
            /// </summary>
            /// <returns></returns>
            public static void ParentGoBack()
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", " if (window.parent) { window.parent.history.back(); }  ", true);
            }

            /// <summary>
            /// Openers the go back.
            /// </summary>
            /// <returns></returns>
            public static void OpenerGoBack()
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "win", " if (window.opener) { window.opener.history.back(); } ", true);
            }
            #endregion
        }
        #endregion

        #region 消息显示类

        /// <summary>
        /// 消息显示类
        /// </summary>
        public class Message
        {
            #region 显示消息对象话
            /// <summary>
            /// 显示提示消息对话框(alert)
            /// </summary>
            /// <param name="msg">消息内容</param>
            public static void ShowMessage(string msg)
            {
                Page page = HttpContext.Current.Handler as Page;
                if (page != null)
                    page.ClientScript.RegisterStartupScript(page.GetType(), "msg", "alert('" + msg.Replace("'","") + "');", true);
            }
            #endregion

            
            #region 显示消息对话框，并关闭当前窗口

            /// <summary>
            /// 显示提示信息，并关闭当前窗口
            /// </summary>
            /// <param name="Msg"></param>
            /// <param name="Isclose"></param>
            public static void ShowMessage(string msg, bool isClose)
            {
                Page page = HttpContext.Current.Handler as Page;
                StringBuilder sb = new StringBuilder();

                sb.Append("alert('" + msg + "');");
                if (isClose)
                {
                    sb.Append("window.close();");
                }
                page.ClientScript.RegisterStartupScript(page.GetType(), "msg", sb.ToString(), true);
            }
            #endregion

            #region 显示消息对话框，并跳转到指定URL

            /// <summary>
            /// 显示提示信息，并跳转到指定的 url
            /// </summary>
            /// <param name="Msg"></param>
            /// <param name="url"></param>
            public static void ShowMessage(string msg, string url)
            {
                Page page = HttpContext.Current.Handler as Page;
                page.ClientScript.RegisterStartupScript(page.GetType(), "msg", "alert('" + msg + "');window.location.href='" + url + "';", true);
            }

            #endregion
        }
        #endregion

    }
}
